package cc.vv.baselibrary.util;

import android.graphics.drawable.Drawable;
import android.support.annotation.FloatRange;
import android.support.annotation.IntRange;
import android.view.animation.Animation;

import cc.vv.lkbasecomponent.util.LKScreenUtil;
import cc.vv.lkimagecomponent2.initplugin.InitPlugin;
import cc.vv.lkimagecomponent2.lib.mode.AnimationMode;
import cc.vv.lkimagecomponent2.lib.mode.DiskCacheMode;
import cc.vv.lkimagecomponent2.lib.mode.PriorityMode;
import cc.vv.lkimagecomponent2.lib.mode.ScaleMode;
import cc.vv.lkimagecomponent2.lib.mode.ShapeMode;
import cc.vv.lkimagecomponent2.loaderplugin.BitmapListener;

/**
 * 图片加载属性
 * Created by sun.li on 2018/5/7.
 *
 * @author sun.li
 */

public class BTImageLoadOption {

    /**
     * 缩略图缩放倍数
     */
    private float thumbnail;
    /**
     * 重写的宽
     */
    private int oWidth;
    /**
     * 重写的高
     */
    private int oHeight;
    /**
     * 是否需要晕映
     */
    private boolean isNeedVignette;
    /**
     * 是否需要素描
     */
    private boolean isNeedSketch;
    /**
     * 马赛克等级
     */
    private float pixelationLevel;
    /**
     * 是否需要马赛克
     */
    private boolean isNeedPixelation;

    /**
     * 是否需要胶片
     */
    private boolean isNeedInvert;
    /**
     * 锐化等级
     */
    private float contrastLevel;
    /**
     * 是否需要锐化
     */
    private boolean isNeedContrast;
    /**
     * 是否需要墨画
     */
    private boolean isNeedSepia;
    /**
     * 是否需要油画
     */
    private boolean isNeedToon;
    /**
     * 是否需要漩涡
     */
    private boolean isNeedSwirl;
    /**
     * 是否需要黑色
     */
    private boolean isNeedGrayscale;
    /**
     * 是否需要亮度
     */
    private boolean isNeedBrightness;
    /**
     * 是否需要亮度
     */
    private float brightnessLeve;
    /**
     * 是否需要模糊
     */
    private boolean needBlur;
    /**
     * 是否过滤颜色
     */
    private boolean needFilteColor;
    /**
     * 过滤颜色
     */
    private int filteColor;

    /**
     * 设置加载紧急程度
     */
    private PriorityMode priority;
    /**
     * 动画类型
     */
    private AnimationMode animationType;
    /**
     * 动画id
     */
    private int animationId;
    /**
     * 动画
     */
    private Animation animation;
    /**
     * 模糊半径
     */
    private int blurRadius;
    /**
     * 占位图
     */
    private int placeHolderResId;
    /**
     * error占位图
     */
    private int errorResId;
    /**
     * 占位图
     */
    private Drawable placeHolderDrawable;
    /**
     * error占位图
     */
    private Drawable errorDrawable;
    /**
     * 是否只获取bitmap
     */
    private boolean asBitmap;
    /**
     * /默认矩形,可选直角矩形,圆形/椭圆
     */
    private ShapeMode shapeMode;
    /**
     * 圆角矩形时圆角的半径
     */
    private int rectRoundRadius;
    /**
     * 磁盘存储模式
     */
    private DiskCacheMode diskCacheStrategy;
    /**
     * 填充模式,默认centercrop,可选fitXY,centerInside...
     */
    private ScaleMode scaleMode;
    /**
     * 如果为gif,动画播放次数.-1:永久 0:不动  N:次数
     */
    private int gifRepeatCount;

    /**
     * 是否crossFade动画
     */
    private boolean isCrossFade;

    /**
     * 是否执行动画
     */
    private boolean isAnim;

    /**
     * fade动画时长，如果为0，则使用glide默认时长
     */
    private int crossFadeDuration;

    public BTImageLoadOption(Builder builder) {
        this.isCrossFade = builder.isCrossFade;
        this.crossFadeDuration = builder.crossFadeDuration;
        this.thumbnail = builder.thumbnail;
        this.oWidth = builder.oWidth;
        this.oHeight = builder.oHeight;
        this.shapeMode = builder.shapeMode;
        if (shapeMode == ShapeMode.RECT_ROUND) {
            this.rectRoundRadius = builder.rectRoundRadius;
        }
        this.scaleMode = builder.scaleMode;
        this.diskCacheStrategy = builder.diskCacheStrategy;
        this.isAnim = builder.isAnimation;
        this.animationId = builder.animationId;
        this.animationType = builder.animationType;
        this.animation = builder.animation;
        this.priority = builder.priority;
        //滤镜
        //是否需要晕映
        this.isNeedVignette = builder.isNeedVignette;
        //是否需要素描
        this.isNeedSketch = builder.isNeedSketch;
        //是否需要马赛克
        this.pixelationLevel = builder.pixelationLevel;
        //是否需要马赛克
        this.isNeedPixelation = builder.isNeedPixelation;
        //是否需要胶片
        this.isNeedInvert = builder.isNeedInvert;
        this.contrastLevel = builder.contrastLevel;
        this.isNeedContrast = builder.isNeedContrast;
        this.isNeedSepia = builder.isNeedSepia;
        this.isNeedToon = builder.isNeedToon;
        this.isNeedSwirl = builder.isNeedSwirl;
        this.isNeedGrayscale = builder.isNeedGrayscale;
        this.isNeedBrightness = builder.isNeedBrightness;
        //是否需要亮度
        this.brightnessLeve = builder.brightnessLeve;
        this.filteColor = builder.filteColor;
        this.needBlur = builder.needBlur;
        this.needFilteColor = builder.needFilteColor;
        this.placeHolderResId = builder.placeHolderResId;
        this.placeHolderDrawable = builder.placeHolderDrawable;
        this.errorDrawable = builder.errorDrawable;
        this.asBitmap = builder.asBitmap;
        this.blurRadius = builder.blurRadius;
        this.errorResId = builder.errorResId;
        this.gifRepeatCount = builder.gifRepeatCount;
    }

    public boolean isAsBitmap() {
        return asBitmap;
    }

    public boolean isCrossFade() {
        return isCrossFade;
    }

    public boolean isAnim() {
        return isAnim;
    }

    public int getGifRepeatCount() {
        return gifRepeatCount;
    }

    public int getCrossFadeDuration() {
        return crossFadeDuration;
    }

    public DiskCacheMode getDiskCacheStrategy() {
        return diskCacheStrategy;
    }

    public int getErrorResId() {
        return errorResId;
    }

    public boolean isNeedBlur() {
        return needBlur;
    }

    public int getPlaceHolderResId() {
        return placeHolderResId;
    }

    public int getRectRoundRadius() {
        return rectRoundRadius;
    }


    public ScaleMode getScaleMode() {
        return scaleMode;
    }

    public ShapeMode getShapeMode() {
        return shapeMode;
    }


    public int getoWidth() {
        return oWidth;
    }

    public int getoHeight() {
        return oHeight;
    }

    public AnimationMode getAnimationType() {
        return animationType;
    }

    public int getAnimationId() {
        return animationId;
    }

    public Animation getAnimation() {
        return animation;
    }

    public PriorityMode getPriority() {
        return priority;
    }

    public int getFilteColor() {
        return filteColor;
    }

    public float getContrastLevel() {
        return contrastLevel;
    }

    public boolean isNeedFilteColor() {
        return needFilteColor;
    }

    public float getBrightnessLeve() {
        return brightnessLeve;
    }

    public boolean isNeedBrightness() {
        return isNeedBrightness;
    }


    public float getThumbnail() {
        return thumbnail;
    }


    public int getBlurRadius() {
        return blurRadius;
    }

    public boolean isNeedGrayscale() {
        return isNeedGrayscale;
    }

    public boolean isNeedSwirl() {
        return isNeedSwirl;
    }

    public Drawable getPlaceHolderDrawable() {
        return placeHolderDrawable;
    }

    public Drawable getErrorDrawable() {
        return errorDrawable;
    }

    public boolean isNeedToon() {
        return isNeedToon;
    }

    public boolean isNeedSepia() {
        return isNeedSepia;
    }

    public boolean isNeedContrast() {
        return isNeedContrast;
    }

    public boolean isNeedInvert() {
        return isNeedInvert;
    }

    public boolean isNeedPixelation() {
        return isNeedPixelation;
    }

    public float getPixelationLevel() {
        return pixelationLevel;
    }

    public boolean isNeedSketch() {
        return isNeedSketch;
    }

    public boolean isNeedVignette() {
        return isNeedVignette;
    }


    public static class Builder {

        private String url;
        private float thumbnail;
        private String filePath;
        private int resId;
        private int rawId;
        private String assertspath;
        private String contentProvider;
        private boolean isGif = false;

        private boolean asBitmap;
        private BitmapListener bitmapListener;
        private int oWidth;

        private int oHeight;
        //滤镜
        private boolean isNeedVignette;

        private boolean isNeedSketch;
        private float pixelationLevel;
        private boolean isNeedPixelation;
        private boolean isNeedInvert;
        private float contrastLevel;
        private boolean isNeedContrast = false;
        private boolean isNeedSepia = false;
        private boolean isNeedToon = false;
        private boolean isNeedSwirl = false;
        private boolean isNeedGrayscale = false;
        private boolean isNeedBrightness = false;
        private float brightnessLeve;
        private boolean needBlur = false;
        private boolean needFilteColor = false;
        private int blurRadius;
        private int placeHolderResId;
        private Drawable placeHolderDrawable;
        private int errorResId;
        private Drawable errorDrawable;
        private ShapeMode shapeMode = ShapeMode.RECT;
        private int rectRoundRadius = 0;
        private DiskCacheMode diskCacheStrategy = InitPlugin.getInstance().mConfig == null ? DiskCacheMode.AUTOMATIC :
                InitPlugin.getInstance().mConfig.getDiskCacheStrategy();
        private ScaleMode scaleMode = ScaleMode.FIT_CENTER;
        private PriorityMode priority = PriorityMode.PRIORITY_IMMEDIATE;
        private int filteColor;
        private int animationId;
        private AnimationMode animationType;
        private Animation animation;
        private boolean isCrossFade = false;
        private int crossFadeDuration = 0;
        private boolean isAnimation = true;
        private int gifRepeatCount = InitPlugin.getInstance().mConfig == null ?
                -1 : InitPlugin.getInstance().mConfig.getGifRepeatCount();

        public BTImageLoadOption build() {
            return new BTImageLoadOption(this);
        }

        /**
         * 添加fade动画,时长是默认
         *
         * @return
         */
        public Builder crossFade() {
            isCrossFade = true;
            return this;
        }

        /**
         * 如果为gif时,播放次数
         *
         * @param count -1:永久  0: 不播放  N:N次
         * @return
         */
        public Builder gifRepeatCount(@IntRange(from = -1) int count) {
            gifRepeatCount = count;
            return this;
        }

        /**
         * 添加fade动画
         *
         * @param duration
         * @return
         */
        public Builder crossFade(int duration) {
            if (duration > 0) {
                isCrossFade = true;
                crossFadeDuration = duration;
            }
            return this;
        }

        /**
         * 缩略图
         *
         * @param thumbnail  0-1:加载全图之前显示缩略图,提升用户体验.值越小,缩略图越模糊,加载越快.
         * @return
         */
        public Builder thumbnail(@FloatRange(from = 0, to = 1) float thumbnail) {
            this.thumbnail = thumbnail;
            return this;
        }

        /**
         * error图
         *
         * @param errorResId
         * @return
         */
        public Builder error(int errorResId) {
            this.errorResId = errorResId;
            return this;
        }

        /**
         * error图
         *
         * @param errorDrawable
         * @return
         */
        public Builder error(Drawable errorDrawable) {
            this.errorDrawable = errorDrawable;
            return this;
        }


        /**
         * 加载图片的分辨率
         *
         * @param oWidth  dp
         * @param oHeight dp
         * @return
         */
        public Builder override(@IntRange(from = 0) int oWidth, @IntRange(from = 0) int oHeight) {
            this.oWidth = LKScreenUtil.dp2px(oWidth);
            this.oHeight = LKScreenUtil.dp2px(oHeight);
            return this;
        }

        /**
         * 占位图
         *
         * @param placeHolderResId
         * @return
         */
        public Builder placeHolder(int placeHolderResId) {
            this.placeHolderResId = placeHolderResId;
            return this;
        }

        /**
         * 占位图
         *
         * @param placeHolderDrawable
         * @return
         */
        public Builder placeHolder(Drawable placeHolderDrawable) {
            this.placeHolderDrawable = placeHolderDrawable;
            return this;
        }

        /**
         * 是否需要高斯模糊
         *
         * @param blurRadius 0-25
         * @return
         */
        public Builder blur(@IntRange(from = 0, to = 25) int blurRadius) {
            if (blurRadius <= 0) {
                return this;
            }
            this.needBlur = true;
            this.blurRadius = blurRadius;
            return this;
        }

        /**
         * 圆角
         *
         * @return
         */
        public Builder asCircle() {
            this.shapeMode = ShapeMode.OVAL;
            return this;
        }

        /**
         * 形状为圆角矩形时的圆角半径
         *
         * @param rectRoundRadius
         * @return
         */
        public Builder rectRoundCorner(@IntRange(from = 0) int rectRoundRadius) {
            if (rectRoundRadius <= 0) {
                return this;
            }
            this.rectRoundRadius = LKScreenUtil.dp2px(rectRoundRadius);
            this.shapeMode = ShapeMode.RECT_ROUND;
            return this;
        }

        /**
         * 正方形
         *
         * @return
         */
        public Builder asSquare() {
            this.shapeMode = ShapeMode.SQUARE;
            return this;
        }

        /**
         * 磁盘缓存
         */
        public Builder diskCacheStrategy(DiskCacheMode diskCacheStrategy) {
            this.diskCacheStrategy = diskCacheStrategy;
            return this;
        }

        /**
         * 拉伸/裁剪模式
         *
         * @param scaleMode 取值ScaleMode
         * @return
         */
        public Builder scale(ScaleMode scaleMode) {
            this.scaleMode = scaleMode;
            return this;
        }

        /**
         * 不执行动画
         *
         * @return
         */
        public Builder dontAnim() {
            this.isAnimation = false;
            return this;
        }

        /**
         * 动画
         *
         * @param animationId
         * @return
         */
        public Builder animate(int animationId) {
            this.animationType = AnimationMode.ANIMATIONID;
            this.animationId = animationId;
            return this;
        }

        /**
         * 动画
         *
         * @param animation
         * @return
         */
        public Builder animate(Animation animation) {
            this.animationType = AnimationMode.ANIMATION;
            this.animation = animation;
            return this;
        }

        /**
         * 加载优先级
         *
         * @param priority
         * @return
         */
        public Builder priority(PriorityMode priority) {
            this.priority = priority;
            return this;
        }

        /**
         * 颜色遮罩
         *
         * @param filteColor
         * @return
         */
        public Builder colorFilter(int filteColor) {
            this.filteColor = filteColor;
            this.needFilteColor = true;
            return this;
        }

        /**
         * 增加亮度
         *
         * @param level brightness value ranges from -1.0 to 1.0, with 0.0 as the normal level
         * @return
         */
        public Builder brightnessFilter(@FloatRange(from = -1f, to = 1f) float level) {
            if (level == 0) {
                return this;
            }
            this.isNeedBrightness = true;
            this.brightnessLeve = level;
            return this;
        }

        /**
         * 灰度化
         *
         * @return
         */
        public Builder grayscaleFilter() {
            this.isNeedGrayscale = true;
            return this;
        }

        /**
         * 旋涡效果
         *
         * @return
         */
        public Builder swirlFilter() {
            this.isNeedSwirl = true;
            return this;
        }

        /**
         * 油画效果
         *
         * @return
         */
        public Builder toonFilter() {
            this.isNeedToon = true;
            return this;
        }

        /**
         * 墨化
         *
         * @return
         */
        public Builder sepiaFilter() {
            this.isNeedSepia = true;
            return this;
        }

        /**
         * 对比度设置
         *
         * @param constrasrLevel ranges from 0.0 to 4.0, with 1.0 as the normal level
         * @return
         */
        public Builder contrastFilter(@FloatRange(from = 0f, to = 4f) float constrasrLevel) {
            if (constrasrLevel <= 0) {
                return this;
            }
            this.contrastLevel = constrasrLevel;
            this.isNeedContrast = true;
            return this;
        }

        /**
         * 胶片效果
         *
         * @return
         */
        public Builder invertFilter() {
            this.isNeedInvert = true;
            return this;
        }

        /**
         * 素描效果
         *
         * @return
         */
        public Builder sketchFilter() {
            this.isNeedSketch = true;
            return this;
        }

        /**
         * 像素画效果
         *
         * @param pixelationLevel 默认是10
         * @return
         */
        public Builder pixelationFilter(float pixelationLevel) {
            if (pixelationLevel <= 0) {
                return this;
            }
            this.pixelationLevel = pixelationLevel;
            this.isNeedPixelation = true;
            return this;
        }

        /**
         * 光晕效果
         *
         * @return
         */
        public Builder vignetteFilter() {
            this.isNeedVignette = true;
            return this;
        }

    }

}
